// pages/member/index.js
var req = require('../../interfaceImpl.js');
var config = require('../../interfaceConfig.js');
var app =getApp();
var code = "";
var token = "";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    name: '15609917408',//用户的名称
    isedit: false,//是否编辑
    menulist: [],//菜单列表
    userinfo:''
  },

  /**
 * 生命周期函数--监听页面加载
 */
  onLoad: function (options) {
       wx.showLoading({title:''});
    var that = this;
    wx.login({
      success: function (res) {
        console.log(res)
        code = res.code;
        return req.MiniAppLogin(code).then(res=>{
          if (res.data.notRegister){//TODO记得修改
             wx.hideLoading();
            wx.redirectTo({
              url: '../regiest/regiest?frommy=true',
            })
          }else{
            token =res.data.data
            //TODO请求用户信息
            app.globalData.hasLogin = true;
            app.globalData.token = token;
            return req.GetUserInfo(token).then(res=>{
                   wx.hideLoading();
              if(res.data.success){
                app.globalData.loginuserinfo = res.data.data // res.data.data
                that.setData({ phone: app.globalData.loginuserinfo.mobile,hasLogin:true})
                console.log(app.globalData.loginuserinfo)
                that.setData({ hasF: true })
                wx.getUserInfo({
                  success: function (res){
                    app.globalData.userinfo = res.userInfo
                    console.log(res)
                    that.setData({ userinfo: app.globalData.userinfo})
                  },fail:function(){
                    //拒绝授权
                  },
                })
              }else{
                wx.showModal({
                  title: '提示',
                  content: '登陆失败,请检查网络',
                })
              }
            })
          }
        })
      }, fail: function (res) {
             wx.hideLoading();
        wx.showModal({
          title: '提示',
          content: '登陆失败',
        })
      }
    })
    //菜单列表数据
    var menulist = [
      {
        id: 1,
        name: '会员二维码',
        subtitle: '扫描二维码',
        imgpath: '/resources/wxcode.png',
        url: '/pages/member/wxcode'
      },
      {
        id: 2,
        name: '在线购票',
        subtitle: '会员在线购票',
        imgpath: '/resources/movie.png',
        url: '/pages/index/index'
      },
      {
        id: 3,
        name: '会员卡',
        subtitle: '查看余额积分',
        imgpath: '/resources/vip.png',
        url: '/pages/member/memberCard'
      },
      {
        id: 4,
        name: '优惠券',
        subtitle: '查看优惠券',
        imgpath: '/resources/quan.png',
        url: 'offerTickets'
      },
      {
        id: 5,
        name: '积分兑换',
        subtitle: '兑换精美礼品',
        imgpath: '/resources/lipin.png',
        url: '/pages/member/duihuanJF'
      },
      {
        id: 6,
        name: '礼品订单',
        subtitle: '核销兑换实物礼品',
        imgpath: '/resources/order.png',
        url: ''
      },
    ];

    that.setData({
      menulist: menulist
    })
    //结束标识符
  },
  /**
   * 提醒
  */
  gotip:function () {
    wx.navigateTo({
      url: '../tip/index',
    })
  },
  /**
   * 设置
  */
  goset:function () {
    wx.navigateTo({
      url: '/pages/setinfo/index',
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  /**
   * 编辑姓名
  */
  editname:function () {
    this.setData({
      isedit: true
    })
  },
  buyTicketsOrder: function () {
    wx.navigateTo({
      url: 'buyTicketsOrder',
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  gochongzhi: function () {
    wx.navigateTo({
      url: 'recharge',
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  gohdorder: function () {
    wx.navigateTo({
      url: 'activityOrder',
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  gosoldorder: function () {
    wx.navigateTo({
      url: 'saleOrder',
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  gojifen: function () {
    wx.navigateTo({
      url: 'userCode',
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
  },

  gomenu:function(){
    wx.reLaunch({
      url: '../index/index',
    })
  },

  goorder: function () {
    wx.navigateTo({
      url: '../member/giftcode',
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  golipin: function () {
    wx.navigateTo({
      url: '../member/duihuanJF',
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
  },

  goquan: function () {
    wx.navigateTo({
      url: '../member/offerTickets',
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
  },

  govip: function () {
    wx.navigateTo({
      url: '../member/memberCard',
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
  },

  goewm: function() {
    wx.navigateTo({
      url: '../member/wxcode',
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
  },

  tobill:function(){
    wx.navigateTo({
      url: '../member/bill',
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
  },

  // 模态弹框
  gokefu: function () {
    
    wx.showModal({
      title: '提示',
      content: '确定拨打电话：4000169103吗？',
      success: function (res) {
        if (res.confirm) {
          console.log('用户点击确定')
          wx.makePhoneCall({
            phoneNumber: '4000169103' 
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },

  gofeedback: function () {
    wx.navigateTo({
      url: '/pages/member/feedback',
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
  },

})